package com.hikeen.launcherx.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.hikeen.launcherx.MyApplication;
import com.hikeen.launcherx.bean.AppInfoss;
import com.hikeen.launcherx.utils.GsonUtils;
import com.hikeen.launcherx.utils.SPUtils;

import java.util.List;

public class AppJTReceiver extends BroadcastReceiver {

    private String replacepackageName;

    @Override
    public void onReceive(Context context, Intent intent) {
        //接收安装广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String packageName = intent.getDataString();
            Log.e("安装了", "onReceive: "+packageName );

            //发消息跟新列表


            MyApplication myApplication = new MyApplication();
            myApplication.testevent(replacepackageName,"addapp");

        }
        //接收卸载广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String packageName = intent.getDataString();

            Log.e("卸载了 :", "onReceive: "+packageName );
            replacepackageName = packageName.replace("package:", "");




            //清除主页面对应的应用
            String savelist = SPUtils.getString("applist", "");
            List<AppInfoss> appInfosses2 = GsonUtils.getPersons(savelist, AppInfoss.class);
            int index = -1 ;
            for (int i = 0; i < appInfosses2.size(); i++) {
                if (replacepackageName.equals(appInfosses2.get(i).getPackageName()) ){
                    index = i;
                }
            }
            if (index>=0){
                appInfosses2.remove(index);
                String s2 = new Gson().toJson(appInfosses2);
                SPUtils.putString("applist",s2);
            }

            //清除最近应用里对应的页面
            String savelist1 = SPUtils.getString("savelist", "");
            List<AppInfoss> appInfosses3 = GsonUtils.getPersons(savelist1, AppInfoss.class);
            int index2 = -1 ;
            for (int i = 0; i < appInfosses3.size(); i++) {
                if (
                        replacepackageName.equals(appInfosses3.get(i).getPackageName()) ){
                    index2 = i;
                }
            }
            if (index2>=0){
                appInfosses3.remove(index2);
                String s3 = new Gson().toJson(appInfosses3);
                SPUtils.putString("savelist",s3);
            }


            MyApplication myApplication = new MyApplication();
            myApplication.testevent(replacepackageName,"remaveapp");








        }
    }
}